package com.wuliu.system.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.constant.CacheConstants;
import com.wuliu.common.core.redis.RedisCache;
import com.wuliu.common.utils.DateUtils;
import javax.annotation.Resource;

import com.wuliu.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.SysWeixinConfigMapper;
import com.wuliu.system.domain.SysWeixinConfig;
import com.wuliu.system.service.ISysWeixinConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 微信参数Service业务层处理
 *
 * @author wuliu
 * @date 2023-10-07
 */
@Service
public class SysWeixinConfigServiceImpl extends ServiceImpl<SysWeixinConfigMapper, SysWeixinConfig> implements ISysWeixinConfigService {
    @Resource
    private SysWeixinConfigMapper sysWeixinConfigMapper;
    @Resource
    private RedisCache redisCache;

    /**
     * 查询微信参数
     *
     * @param id 微信参数主键
     * @return 微信参数
     */
    @Override
    public SysWeixinConfig selectSysWeixinConfigById(String id) {
        return sysWeixinConfigMapper.selectSysWeixinConfigById(id);
    }

    /**
     * 查询微信参数列表
     *
     * @param sysWeixinConfig 微信参数
     * @return 微信参数
     */
    @Override
    public List<SysWeixinConfig> selectSysWeixinConfigList(SysWeixinConfig sysWeixinConfig) {
        return sysWeixinConfigMapper.selectSysWeixinConfigList(sysWeixinConfig);
    }

    /**
     * 新增微信参数
     *
     * @param sysWeixinConfig 微信参数
     * @return 结果
     */
    @Override
    public int insertSysWeixinConfig(SysWeixinConfig sysWeixinConfig) {
        sysWeixinConfig.setCreateTime(DateUtils.getNowDate());
        return sysWeixinConfigMapper.insertSysWeixinConfig(sysWeixinConfig);
    }

    /**
     * 修改微信参数
     *
     * @param sysWeixinConfig 微信参数
     * @return 结果
     */
    @Override
    public int updateSysWeixinConfig(SysWeixinConfig sysWeixinConfig) {
        sysWeixinConfig.setUpdateTime(DateUtils.getNowDate());
        return sysWeixinConfigMapper.updateSysWeixinConfig(sysWeixinConfig);
    }

    /**
     * 批量删除微信参数
     *
     * @param ids 需要删除的微信参数主键
     * @return 结果
     */
    @Override
    public int deleteSysWeixinConfigByIds(String[] ids) {
        return sysWeixinConfigMapper.deleteSysWeixinConfigByIds(ids);
    }

    /**
     * 删除微信参数信息
     *
     * @param id 微信参数主键
     * @return 结果
     */
    @Override
    public int deleteSysWeixinConfigById(String id) {
        return sysWeixinConfigMapper.deleteSysWeixinConfigById(id);
    }

    /**
     * 加载微信配置到缓存
     *
     * @param sysAccount
     */
    @Override
    public void loadConfigCache(String sysAccount) {
        QueryWrapper<SysWeixinConfig> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(sysAccount)) {
            wrapper.eq("sys_account", sysAccount);
        }
        List<SysWeixinConfig> params = sysWeixinConfigMapper.selectList(wrapper);
        params.forEach(t -> redisCache.setCacheObject(CacheConstants.WEIXIN_CONFIG_KEY + t.getAccount(), JSONObject.toJSONString(t)));
    }
}
